
// === uuMeta.className ===
// depend: uuMeta, uuMix.string

(function uuMetaClassNameScope() {
var _doc = document,
    _SPLIT_SPACE = /\s+/,
    _TO_ONE_SPACE = /\s+/g;

// inner - make multiple className RegExp object
function multiClassNameRex(ary) { // @param Array:
                                  // @return RegExpObject:
  return RegExp("(?:^| )(" + ary.join("|") + ")(?:$|(?= ))", "g");
}

// uuMeta.className.has - has className
function hasClass(node,        // @param Node:
                  className) { // @param JointString: "name" or
                               //                     "name1 name2 ..."
                               // @return Boolean: true = has className
  var ary = className.trim().split(_SPLIT_SPACE), rex, match,
      cn = node.className;

  if (!cn) {
    return false;
  }
  if (ary.length > 1) {
    match = cn.match(multiClassNameRex(ary));
    return match && match.length >= ary.length;
  }
  return (" " + cn + " ").indexOf(" " + ary[0] + " ") >= 0;
}

// uuMeta.className.add - add className property
function addClass(node,        // @param Node:
                  className) { // @param JointString: "name" or
                               //                     "name1 name2 ..."
  node.className =
      (node.className + " " + className).replace(_TO_ONE_SPACE, " ").trim();
}

// uuMeta.className.remove - remove className property
function removeClass(node,        // @param Node:
                     className) { // @param JointString: "name" or
                                  //                     "name1 name2 ..."
  var rex = multiClassNameRex(className.trim().split(_SPLIT_SPACE));

  node.className = node.className.replace(rex, "").trim();
}

// uuMeta.className.toggle - add className property or remove
function toggleClass(node,        // @param Node:
                     className) { // @param JointString: "name" or
                                  //                     "name1 name2 ..."
  (hasClass(node, className) ? removeClass : addClass)(node, className);
}

// --- initialize ---

// --- export ---
uuMeta.className = {
  has:    hasClass,
  add:    addClass,
  remove: removeClass,
  toggle: toggleClass
};

})(); // uuMeta.className scope

